package com.xteam.xnetworkdisk.config;

import com.xteam.xnetworkdisk.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.*;

@Slf4j
@Component
public class DirectoryInitializer implements ApplicationListener<ApplicationReadyEvent> {

    private static final String DIRECTORY_PATH = System.getProperty("user.home") + File.separator + "xnetworkdisk";
    private static final String TOOLS_SUBDIRECTORY_PATH = DIRECTORY_PATH + File.separator + "tools";
    private static final String TEMP_PATH = DIRECTORY_PATH + File.separator + "temp";
    private static final String FILE_PATH = DIRECTORY_PATH + File.separator + "file";
    private static final String RESOURCE_IP_PATH = "static/ip2region.xdb"; // 资源文件在 resources/static 下
    private static final String RESOURCE_CONVERT_TO_M3U8_WINDOWS_PATH = "static/convert_to_m3u8.bat"; // 资源文件在 resources/static 下
    private static final String RESOURCE_CONVERT_TO_M3U8_LINUX_PATH = "static/convert_to_m3u8.sh"; // 资源文件在 resources/static 下

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        createDirectoryAndFile();
    }

    private String getFullPath(String sourcePath , String folderName) {
        return sourcePath + File.separator + folderName;
    }

    private void createDirectory(String path) {
        log.info("Creating directory: " + path);
        // 创建目录
        File directory = new File(path);
        if (!directory.exists()) {
            boolean created = directory.mkdirs();
            if (!created) {
                System.err.println("Failed to create directory: " + path);
                return;
            }
        }
    }
    private void createDirectoryAndFile() {

        createDirectory(DIRECTORY_PATH);
        createDirectory(TOOLS_SUBDIRECTORY_PATH);

        // 复制资源文件到 ip 目录
        copyResourceToDirectory(RESOURCE_IP_PATH, TOOLS_SUBDIRECTORY_PATH);
        copyResourceToDirectory(RESOURCE_CONVERT_TO_M3U8_WINDOWS_PATH, TOOLS_SUBDIRECTORY_PATH);
        copyResourceToDirectory(RESOURCE_CONVERT_TO_M3U8_LINUX_PATH, TOOLS_SUBDIRECTORY_PATH);

        createDirectory(TEMP_PATH);
        createDirectory(FILE_PATH);

        createDirectory(getFullPath(FILE_PATH,Constant.USER_FILE_BUKET));
        createDirectory(getFullPath(FILE_PATH,Constant.USER_AVATAR_BUKET));
        createDirectory(getFullPath(FILE_PATH,Constant.USER_BACKGROUND_BUKET));
        createDirectory(getFullPath(FILE_PATH,Constant.USER_TS_BUKET));

        log.info("File written successfully");

    }

    private void copyResourceToDirectory(String resourceFilePath, String targetDirectoryPath) {
        ClassPathResource resource = new ClassPathResource(resourceFilePath);
        File targetFile = new File(targetDirectoryPath, new File(resourceFilePath).getName());

        try (InputStream inputStream = resource.getInputStream();
             FileOutputStream outputStream = new FileOutputStream(targetFile)) {

            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }

            log.info("Resource file copied to: " + targetFile.getAbsolutePath());

        } catch (IOException e) {
            log.error("Failed to copy resource file: " + resourceFilePath + " to " + targetDirectoryPath, e);
        }
    }

}
